package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.constant.MessageConstant;
import com.atguigu.pojo.Member;
import com.atguigu.pojo.TravelGroup;
import com.atguigu.pojo.TravelItem;
import com.atguigu.service.TravelGroupService;
import com.atguigu.vo.PageResult;
import com.atguigu.vo.QueryPageBean;
import com.atguigu.vo.Result;
import com.github.pagehelper.PageHelper;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/TravelGroup")
public class TravelGroupController {

    @Reference
    TravelGroupService travelGroupService;

    @PostMapping("/add")
    public Result add(Integer[] travelItemIds, @RequestBody TravelGroup travelGroup){
        try {
            travelGroupService.add(travelItemIds,travelGroup);
            return new Result(true, MessageConstant.ADD_TRAVELGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_TRAVELGROUP_FAIL);
        }

    }

    @PostMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
            PageResult pageResult = travelGroupService.findPage(queryPageBean.getCurrentPage(),
                    queryPageBean.getPageSize(),
                    queryPageBean.getQueryString());
            return pageResult;
    }

    @GetMapping("/get")
    public Result get(Integer id){
        try {
            TravelGroup travelGroup = travelGroupService.get(id);
            return new Result(true,MessageConstant.QUERY_TRAVELGROUP_SUCCESS,travelGroup);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_TRAVELGROUP_FAIL);
        }
    }

    @GetMapping("/getTravelItemIdByTravelGroupId")
    public Result getTravelItemIdByTravelGroupId(Integer id){
        try {
            Integer[] travelItemId = travelGroupService.getTravelItemIdByTravelGroupId(id);
            return new Result(true,MessageConstant.QUERY_TRAVELITEM_SUCCESS,travelItemId);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_TRAVELITEM_FAIL);
        }
    }

    @PostMapping("/update")
    public Result update(Integer[] travelItemIds,@RequestBody TravelGroup travelGroup){
        try {
            travelGroupService.update(travelItemIds,travelGroup);
            return new Result(true,MessageConstant.EDIT_TRAVELGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_TRAVELGROUP_FAIL);
        }
    }

    @GetMapping("/deleteTravelGroup")
    public Result deleteTravelGroup(Integer id){
        try {
            travelGroupService.deleteTravelGroup(id);
            return new Result(true,MessageConstant.DELETE_TRAVELGROUP_SUCCESS);
        } catch (RuntimeException e){
            return new Result(false,"有属性关联，请先解除关联");
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_TRAVELGROUP_FAIL);
        }
    }

    @GetMapping("/findAll")
    public Result findAll(){
        try {
            List<TravelGroup> travelGroups = travelGroupService.findAll();
            return new Result(true,MessageConstant.QUERY_TRAVELGROUP_SUCCESS,travelGroups);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_TRAVELGROUP_FAIL);
        }
    }
}
